package com.mm.service.impl;

import com.mm.bo.EmployeeInsertBo;
import com.mm.bo.EmployeeSearchBo;
import com.mm.bo.EmployeeUpdateBo;
import com.mm.mapper.EmployeeInfoMapper;
import com.mm.mapper.LogsInfoMapper;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.AdministratorInfo;
import com.mm.pojo.EmployeeInfo;
import com.mm.pojo.LogsInfo;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.EmployeeInfoService;
import com.mm.util.PageUtil;
import com.mm.vo.EmployeeInfoDetailsVo;
import com.mm.vo.EmployeeInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName EmployeeServiceImpl
 * @Description 员工信息 Service实现类
 * @Author 任昊
 * @Date 2020/12/4 10:12
 */
@Service
public class EmployeeInfoServiceImpl implements EmployeeInfoService {


    @Resource
    private EmployeeInfoMapper employeeInfoMapper;

    @Resource
    private Sid sid;

    @Resource
    private RedisService redisService;

    @Resource
    private LogsInfoMapper logsInfoMapper;

    /**
     * 新增员工信息
     *
     * @param employeeInsertBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertEmoloyeeInfo(EmployeeInsertBo employeeInsertBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, employeeInsertBo.getToken(), AdministratorInfo.class);

        EmployeeInfo employeeInfo = new EmployeeInfo();
        BeanUtils.copyProperties(employeeInsertBo, employeeInfo);
        long currentTimeMillis = System.currentTimeMillis();
        employeeInfo.setEmpCreateTime(currentTimeMillis);
        employeeInfo.setEmpUpdateTime(currentTimeMillis);
        employeeInfo.setEmpId(sid.nextShort());
        int row = employeeInfoMapper.insertSelective(employeeInfo);

        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "新增了员工：" + employeeInsertBo.getEmpName();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, currentTimeMillis, currentTimeMillis);
        logsInfoMapper.insertSelective(logsInfo);

        return row;
    }

    /**
     * 查询员工信息(工号、姓名、入职时间)
     *
     * @param employeeSearchBo
     * @return
     */
    @Override
    public PageUtil<EmployeeInfoVo> selectEmployeeInfo(EmployeeSearchBo employeeSearchBo) {
        //查询员工信息总条数
        Integer count = employeeInfoMapper.findEmployeeCount(employeeSearchBo);
        //查询员工信息列表
        List<EmployeeInfoVo> employeeInfo = employeeInfoMapper.findEmployeeInfo(employeeSearchBo);
        PageUtil<EmployeeInfoVo> pageUtil = new PageUtil<>(count, count, employeeSearchBo.getPageIndex(), employeeSearchBo.getPageSize(), employeeInfo);
        return pageUtil;
    }

    /**
     * 修改员工信息
     *
     * @param employeeUpdateBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateEmoloyeeInfo(EmployeeUpdateBo employeeUpdateBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, employeeUpdateBo.getToken(), AdministratorInfo.class);

        EmployeeInfo employeeInfo = new EmployeeInfo();
        BeanUtils.copyProperties(employeeUpdateBo, employeeInfo);
        Example example = new Example(EmployeeInfo.class);
        example.createCriteria().andEqualTo("empId", employeeUpdateBo.getEmpId());
        int row = employeeInfoMapper.updateByExampleSelective(employeeInfo, example);

        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改了员工信息：" + employeeUpdateBo.getEmpId();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }

    /**
     * 删除员工信息  逻辑删除
     *
     * @param empId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteEmoloyeeInfo(String empId, String token) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, token, AdministratorInfo.class);

        EmployeeInfo employeeInfo = new EmployeeInfo();

        employeeInfo.setEmpId(empId);
        employeeInfo.setEmpEmpStatus((long) 0);
        int rowEmp = employeeInfoMapper.updateByPrimaryKeySelective(employeeInfo);

        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "删除了员工：" + empId;
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);

        return rowEmp;
    }

    /**
     * 查询员工详情信
     *
     * @param empId
     * @return
     */
    @Override
    public EmployeeInfoDetailsVo selectEmployeeInfoDetails(String empId) {
        EmployeeInfoDetailsVo employeeInfoDetailsVo = employeeInfoMapper.findEmployeeInfoDetails(empId);
        employeeInfoDetailsVo.setEmpAssVos(employeeInfoMapper.queryEmpAss(empId));

        return employeeInfoDetailsVo;
    }

    /**
     * 修改员工在职离职状态
     *
     * @param empId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer dimissionEmoloyeeInfo(String empId, String token) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, token, AdministratorInfo.class);

        EmployeeInfo employeeInfo = new EmployeeInfo();
        employeeInfo.setEmpId(empId);
        employeeInfo.setEmpWorkStatus((long) 0);
        Integer row = employeeInfoMapper.updateByPrimaryKeySelective(employeeInfo);

        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改了员工的在职离职状态：" + empId;
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }
}


